﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.SessionState;


namespace UI.Admin.User
{
    /// <summary>
    /// AjaxAction 的摘要说明
    /// </summary>
    public class AjaxAction : IHttpHandler, IRequiresSessionState
    {

        public void ProcessRequest(HttpContext context)
        {
            // 验证
            //string auth = Utility.Authorization.Check(HttpContext.Current.Session["role"], "UserAdmin", "");
            //if (auth != "ACCEPT")
            //{
           //     return;
           // }
            context.Response.ContentType = "text/plain";
            string dowhat = context.Request["dowhat"];
            if (dowhat == null) dowhat = "";

            // 操作用户及IP
            string UserName = "test";//((Models.User)(HttpContext.Current.Session["user"])).Name;
            string UserIP = "1314";//context.Request.ServerVariables["LOCAl_ADDR"];

            #region 添加用户
            if (dowhat == "add_user")
            {
                string del = "  ";
                string Name = context.Request["Name"].Trim(del.ToCharArray());
                string Password = context.Request["Password"];
                string RoleID = context.Request["RoleID"];
                string UserEnabled = context.Request["UserEnabled"];

                if (BLL.Create.CreateUser(Name, Password, RoleID, UserEnabled) > 0)
                {
                    // Log
                    BLL.Create.CreateLogRecord(UserName, UserIP, "添加一个名为‘" + Name + "’的用户");
                    context.Response.Write("success");
                }
                else
                {
                    // Log
                    context.Response.Write("failed");
                }
                context.Response.End();
                return;
            }
            #endregion

            #region 删除用户
            if (dowhat == "delete_user")
            {
                string ID = context.Request["ID"];

                string Name = BLL.Find.FindUser(ID).Name;

                if (BLL.Delete.DeleteUser(ID) > 0)
                {
                    // Log
                    BLL.Create.CreateLogRecord(UserName, UserIP, "删除了名为‘" + Name + "’用户");
                    context.Response.Write("success");
                }
                else
                {
                    // Log
                    context.Response.Write("failed");
                }
                context.Response.End();
                return;
            }
            #endregion

            #region 更新用户
            if (dowhat == "update_user")
            {
                string ID = context.Request["ID"];
                string Name = context.Request["Name"];
                string Password = context.Request["Password"];
                string RoleID = context.Request["RoleID"];
                string UserEnabled = context.Request["UserEnabled"];

                if (BLL.Update.UpdateUser(ID, Name,Password, RoleID, UserEnabled) > 0)
                {
                    // Log
                    BLL.Create.CreateLogRecord(UserName, UserIP, "修改了名为‘" + Name + "的'用户");
                    context.Response.Write("success");
                }
                else
                {
                    // Log
                    context.Response.Write("failed");
                }
                context.Response.End();
                return;
            }
            #endregion

        }
        

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}